package win;

/**
 * Created with IntelliJ IDEA.
 * ClassName: BinarySearch
 * Package: win
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-08-21
 * Time: 19:43
 */

//二分查找
public class BinarySearch {
    public static void main(String[] args) {
        //二分查找前提是有序的
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};

        int tmp = 20;

        int head = 0;
        int end = arr.length - 1;
        boolean is = false;

        while (head <= end) {
            int z = (head + end) / 2;
            if (arr[z] == tmp) {
                System.out.println("找到了" + tmp + " 下标是" + z);
                //设置一个标志位 表示没找到
                is = true;
                break;
            } else if (arr[z] > tmp) {
                end = z - 1;
            } else {
                head = z + 1;
            }
        }

        if (!is){
            System.out.println("没找到");
        }
    }
}
